
#include "game.h"

game::game(QWidget *parent)
    : QMainWindow{parent}
{
    this->setWindowTitle("2048");
    this->setFixedSize(1150,700);
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            s[i][j]=0;
        }
    }

    button = new QPushButton("开始游戏",this);
    button->setGeometry(900,320,150,80);
    button->setStyleSheet("background-color:white");
    srand(uint(QTime(0,0,0).secsTo(QTime::currentTime())));
    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));
}

void game::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    QPixmap pixmap = QPixmap(":/background.icon").scaled(this->size());
     p.drawPixmap(this->rect(), pixmap);
     p.setBrush(Qt::white);
     p.setFont(QFont("楷体",18,500,false));
    QString strscore;
    p.drawText(QPoint(910,200),"分数: ");
    p.drawText(QPoint(910,280),QString::number(score));
    for (int i=0;i<4;i++) {
        for (int j=0;j<4;j++) {
            p.setPen(Qt::transparent);//?

            if(s[i][j]==0){
                 p.setBrush(QColor(255,250,222,255));
                p.drawRect(i*80+550,j*80+100,75,75);
            }
            else if (s[i][j]==2) {
                           p.setBrush(QColor(255,229,168,255));
                           p.drawRect(i*80+550,j*80+100,75,75);
                           p.setPen(Qt::black);
                           p.setFont(QFont("微软雅黑",10,700,false));
                           p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(2),QTextOption(Qt::AlignCenter));

                       }
                       else if (s[i][j]==4) {
                           p.setBrush(QColor(255,197,136,255));
                           p.drawRect(i*80+550,j*80+100,75,75);
                           p.setPen(Qt::black);
                           p.setFont(QFont("微软雅黑",10,700,false));
                           p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(4),QTextOption(Qt::AlignCenter));
                       }
                       else if (s[i][j]==8) {
                           p.setBrush(QColor(251,179,129,255));
                           p.drawRect(i*80+550,j*80+100,75,75);
                           p.setPen(Qt::black);
                           p.setFont(QFont("微软雅黑",10,700,false));
                           p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(8),QTextOption(Qt::AlignCenter));
                       }

            else if (s[i][j]==16) {
                            p.setBrush(QColor(250,160,99,255));
                            p.drawRect(i*80+550,j*80+100,75,75);
                            p.setPen(Qt::black);
                            p.setFont(QFont("微软雅黑",10,700,false));
                            p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(16),QTextOption(Qt::AlignCenter));
                        }
                        else if (s[i][j]==32) {
                            p.setBrush(QColor(251,103,55,255));
                            p.drawRect(i*80+550,j*80+100,75,75);
                            p.setPen(Qt::black);
                            p.setFont(QFont("微软雅黑",10,700,false));
                            p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(32),QTextOption(Qt::AlignCenter));
                        }
                        else if (s[i][j]==64) {
                            p.setBrush(QColor(213,92,14,255));
                            p.drawRect(i*80+550,j*80+100,75,75);
                            p.setPen(Qt::black);
                            p.setFont(QFont("微软雅黑",10,700,false));
                            p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(64),QTextOption(Qt::AlignCenter));
            }
                        else if (s[i][j]==128) {
                            p.setBrush(QColor(252,161,159,255));
                            p.drawRect(i*80+550,j*80+100,75,75);
                            p.setPen(Qt::black);
                            p.setFont(QFont("微软雅黑",10,700,false));
                            p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(128),QTextOption(Qt::AlignCenter));
                        }
                        else if (s[i][j]==256) {
                            p.setBrush(QColor(248,126,124,147));
                            p.drawRect(i*80+550,j*80+100,75,75);
                            p.setPen(Qt::black);
                            p.setFont(QFont("微软雅黑",10,700,false));
                            p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(256),QTextOption(Qt::AlignCenter));
                        }
                        else if (s[i][j]==512) {
                            p.setBrush(QColor(255,50,80,255));
                            p.drawRect(i*80+550,j*80+100,75,75);
                            p.setPen(Qt::black);
                            p.setFont(QFont("微软雅黑",10,700,false));
                            p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(512),QTextOption(Qt::AlignCenter));
                        }
            else if (s[i][j]==1024) {
                         p.setBrush(QColor(188,25,22,255));
                         p.drawRect(i*80+550,j*80+100,75,75);
                         p.setPen(Qt::black);
                         p.setFont(QFont("微软雅黑",10,700,false));
                         p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(1024),QTextOption(Qt::AlignCenter));
                     }
                     else if (s[i][j]==2048) {
                         p.setBrush(QColor(245,69,67,15));
                         p.drawRect(i*80+550,j*80+100,75,75);
                         p.setPen(Qt::black);
                         p.setFont(QFont("微软雅黑",10,700,false));
                         p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(2048),QTextOption(Qt::AlignCenter));
                     }
                     else{
                         p.setBrush(Qt::darkBlue);
                         p.drawRect(i*80+550,j*80+100,75,75);
                         p.setPen(Qt::black);
                         p.setFont(QFont("微软雅黑",10,700,false));
                         p.drawText(QRectF(i*80+550,j*80+100,75,75),QString::number(s[i][j]),QTextOption(Qt::AlignCenter));
                     }
                 }
             }
         }
void game::keyPressEvent(QKeyEvent *event)
{
    if(!state){
        return ;
    }
        switch (event->key()) {
        case Qt::Key_W:
            PressUp();
            break;

        case Qt::Key_S:
            PressDown();
            break;

        case Qt::Key_A:
            PressLeft();
            break;

        case Qt::Key_D:
            PressRight();
            break;

        default:
                   return;
               }
               myRand();
               update();
       }
       void game::slotStart(){
           QMessageBox::about(this,"游戏规则","当数字相同是会合并并加分，若无法移动则游戏失败");
           score=0;
           for (int i=0;i<4;i++) {
               for (int j=0;j<4;j++) {
                   s[i][j]=0;
               }
           }
           button->setText("重新游戏");
           button->setStyleSheet("background-color:white");

           int randi=rand()%4;
           int randj=rand()%4;
           s[randi][randj]=2;

           state=true;
               update();
           }
           void game::PressUp(){
               for (int i=0;i<4;i++) {
                   for (int j=1;j<4;j++) {
                       if(s[i][j]==0)continue;
                       for (int p=0;p<j;p++) {
                           if(s[i][p]==0){
                               s[i][p]=s[i][j];
                               s[i][j]=0;
                               break;
                           }
                       }
                   }
               }
                   for (int i=0;i<4;i++) {
                       for (int j=0;j<3;j++) {
                           if(s[i][j]==s[i][j+1]){
                               s[i][j]=2*s[i][j];
                               s[i][j+1]=0;
                               score+=s[i][j];
                               for (int p=j+2;p<4;p++) {
                                   s[i][p-1]=s[i][p];
                               }
                           }
                       }
                   }
               }
               void game::PressDown(){
                   for (int i=0;i<4;i++) {
                       for (int j=2;j>=0;j--) {
                           if(s[i][j]==0)continue;
                           for (int p=3;p>j;p--) {
                               if(s[i][p]==0){
                                   s[i][p]=s[i][j];
                                   s[i][j]=0;
                                   break;

                               }
                            }
                        }
                    }
                    for (int i=0;i<4;i++) {
                        for (int j=3;j>0;j--) {
                            if(s[i][j]==s[i][j-1]){
                                s[i][j]=2*s[i][j];
                                s[i][j-1]=0;
                                score+=s[i][j];
                                for (int p=j-2;p>=0;p--) {
                                    s[i][p+1]=s[i][p];
                                }
                            }
                        }
                    }
                }
                void game::PressLeft(){
                    for (int j=0;j<4;j++) {
                        for (int i=1;i<4;i++) {
                            if(s[i][j]==0){
                                continue;
                            }
                            for (int p=0;p<i;p++) {
                                if(s[p][j] == 0){
                                                   s[p][j] = s[i][j];
                                                   s[i][j] = 0;
                                                   break;
                                               }
                                           }
                                       }
                                   }
                                   //相加
                                   for (int j=0;j<4;j++) {
                                       for (int i=0;i<3;i++) {
                                           if(s[i][j]==s[i+1][j]){
                                               s[i][j]=s[i][j]*2;
                                               score+=s[i][j];
                                               s[i+1][j]=0;
                                               for(int p=i+2;p<4;p++){
                                                   s[p-1][j] = s[p][j];
                                              }
                                           }
                                       }
                                   }
                               }

                void game::PressRight(){
                    for (int j=0;j<4;j++) {
                        for (int i=2;i>=0;i--) {
                            if(s[i][j]==0){
                                continue;
                            }
                            for (int p=3;p>i;p--) {
                                if(s[p][j] == 0){
                                    s[p][j] = s[i][j];
                                    s[i][j] = 0;
                                    break;
                                }
                            }
                        }
                    }
                    for (int j=0;j<4;j++) {
                        for (int i=3;i>=0;i--) {
                            if(s[i][j]==s[i-1][j]){
                                s[i][j]=s[i][j]*2;
                                s[i-1][j]=0;
                                score+=s[i][j];
                                for(int p=i-2;p>=0;p--){
                                    s[p+1][j] = s[p][j];
                                }
                            }
                        }
                    }
                }
                void game::myRand(){
                    int i=0,j=0;
                    struct Ns n[15];
                    int ni=0;
                    for (i=0;i<4;i++) {
                        for (j=0;j<4;j++) {
                            if(s[i][j]==0){
                                n[ni].i=i;
                                n[ni].j=j;
                                ni++;
                            }
                        }
                    }

                    if (ni==0) {
                        for (i=0;i<4;i++) {
                            for (j=0;j<3;j++) {
                                if(s[i][j]==s[i][j+1]){
                                    return;
                                }
                            }
                        }
                           for (j=0;j<4;j++) {
                               for (i=0;i<3;i++) {
                                   if(s[i][j]==s[i+1][j]){
                                       return;
                                   }
                               }
                           }
                           QMessageBox::about(this,"游戏结束","当前分："+QString::number(score)+" ");
                           return;
                       }
                       int rand1=rand()%ni;
                       s[n[rand1].i][n[rand1].j]=2;
                   }
